package com.java;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressAPI {
    public static void main(String[] args) throws UnknownHostException {
        // getLocalHost：获取本机InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost); // CJ/192.168.150.1

        // getByName：获取InetAddress对象，参数可为IP地址或主机名称。底层自动判断协议类型
        InetAddress inetAddress = InetAddress.getByName("CJ");
        System.out.println(inetAddress); // CJ/192.168.150.1

        // getHostName：获取主机名称
        System.out.println(inetAddress.getHostName()); // CJ

        // getHostAddress：获取IP地址
        System.out.println(inetAddress.getHostAddress()); // 192.168.150.1
    }
}
